﻿using UnityEditor;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;

class LuaPostprocessor : AssetPostprocessor
{
    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        var setting = AssetDatabase.LoadAssetAtPath<AddressableAssetSettings>("Assets/AddressableAssetsData/AddressableAssetSettings.asset");

        var group = setting.FindGroup(AddressablesConfig.LuaGroup);

        foreach (string importedAsset in importedAssets)
        {
            if (importedAsset.EndsWith(GameConfig.LuaExt))
            {
                //其他路径的Lua不处理
                var str = importedAsset.Replace(@"\", "/");
                if (!str.Contains(AppStartupLua.pathPrefix))
                {
                    Debug.Log(str + " ignore");
                    continue;
                }

                var guid = AssetDatabase.AssetPathToGUID(str); //获得GUID

                var entry = setting.CreateOrMoveEntry(guid, group); //通过GUID创建entry

                entry.SetLabel(AddressablesConfig.LuaLabel, true);
                entry.SetAddress(entry.AssetPath);
                Debug.Log("AddressableAssetSettings Lua:" + str);
            }
        }

    }

}